package zjc.bookkeeping.service.imp;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import zjc.bookkeeping.activity.SigninActivity;
import zjc.bookkeeping.model.Account;
import zjc.bookkeeping.model.User;
import zjc.bookkeeping.server.MyHttpUtil;
import zjc.bookkeeping.service.UserService;

public class UserServiceImp implements UserService {
//    private SigninActivity signinActivity;
    private User resultUser = new User();

//    public UserServiceImp(SigninActivity signinActivity) {
//        this.signinActivity = signinActivity;
//    }

    @Override
    public User loginByPhone(String phone) {
        Log.i("phone",phone+"----————————————————————————————————————————————"+phone);
        String url = MyHttpUtil.host + "/user/loginByPhone";
        FormBody.Builder params = new FormBody.Builder();
        params.add("phone",phone);
        Request request = new Request.Builder()
                .url(url)
                .post(params.build())
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String data = jsonObject.optString("data",null);
                    Log.i(data,data);
                    if (!data.isEmpty()){
                        Log.i("data",data);
                        JSONObject object = new JSONObject(data);
                        int id = object.optInt("userId",0);
                        String name = object.optString("userName",null);
                        String photo = object.optString("userPhoto",null);
                        String phone = object.optString("userPhone",null);
                        String qq = object.optString("userQq",null);
                        String zfb = object.optString("userZfb",null);
                        resultUser.setUserId(id);
                        resultUser.setUserName(name);
                        resultUser.setUserPhoto(photo);
                        resultUser.setUserPhone(phone);
                        resultUser.setUserQq(qq);
                        resultUser.setUserZfb(zfb);
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url,params.build(), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String data = jsonObject.optString("data",null);
//                    Log.i(data,data);
//                    if (!data.isEmpty()){
//                        Log.i("data",data);
//                        JSONObject object = new JSONObject(data);
//                        int id = object.optInt("userId",0);
//                        String name = object.optString("userName",null);
//                        String photo = object.optString("userPhoto",null);
//                        String phone = object.optString("userPhone",null);
//                        String qq = object.optString("userQq",null);
//                        String zfb = object.optString("userZfb",null);
//                        resultUser.setUserId(id);
//                        resultUser.setUserName(name);
//                        resultUser.setUserPhoto(photo);
//                        resultUser.setUserPhone(phone);
//                        resultUser.setUserQq(qq);
//                        resultUser.setUserZfb(zfb);
//                        Log.i("resultUser", String.valueOf(resultUser));
//                        User user = resultUser;
//                        Log.i("user", user.getUserName());
//                        if (resultUser != null){
//                            Log.i("user", user.getUserName());
//                            signinActivity.loginCallback(resultUser);
//                        }
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        Log.i("---","--------第二次————————————————");
        return resultUser;
    }

    @Override
    public User loginByZfb(String authCode) {
        Log.i("authCode",authCode);
        String url = MyHttpUtil.host + "/user/loginByzfb";
//        url += "?authCode=" + authCode;
        FormBody.Builder params = new FormBody.Builder();
        params.add("authCode",authCode);
        Request request = new Request.Builder()
                .url(url)
                .post(params.build())
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String data = jsonObject.optString("data",null);
                    Log.i(data,data);
                    if (!data.isEmpty()){
                        Log.i("data",data);
                        JSONObject object = new JSONObject(data);
                        int id = object.optInt("userId",0);
                        String name = object.optString("userName",null);
                        String photo = object.optString("userPhoto",null);
                        String phone = object.optString("userPhone",null);
                        String qq = object.optString("userQq",null);
                        String zfb = object.optString("userZfb",null);
                        resultUser.setUserId(id);
                        resultUser.setUserName(name);
                        resultUser.setUserPhoto(photo);
                        resultUser.setUserPhone(phone);
                        resultUser.setUserQq(qq);
                        resultUser.setUserZfb(zfb);
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url,params.build(), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String data = jsonObject.optString("data",null);
//                    Log.i(data,data);
//                    if (!data.isEmpty()){
//                        Log.i("data",data);
//                        JSONObject object = new JSONObject(data);
//                        Log.i("object", String.valueOf(object));
//                        int id = object.optInt("userId",0);
//                        String name = object.optString("userName",null);
//                        String photo = object.optString("userPhoto",null);
//                        String phone = object.optString("userPhone",null);
//                        String qq = object.optString("userQq",null);
//                        String zfb = object.optString("userZfb",null);
//                        resultUser.setUserId(id);
//                        resultUser.setUserName(name);
//                        resultUser.setUserPhoto(photo);
//                        resultUser.setUserPhone(phone);
//                        resultUser.setUserQq(qq);
//                        resultUser.setUserZfb(zfb);
//                        Log.i("resultUser", String.valueOf(resultUser));
//                        User user = resultUser;
//                        Log.i("user", user.getUserName());
//                        if (resultUser != null){
//                            Log.i("user", user.getUserName());
//                            signinActivity.loginCallback(resultUser);
//                        }
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        Log.i("---","--------第二次————————————————");
        return resultUser;
    }

    @Override
    public User loginByQq(String name,String qq) {
        String url = MyHttpUtil.host + "/user/loginByQq";
//        url += "?name=" + name;
//        url += "&qq=" +qq;
        FormBody.Builder params = new FormBody.Builder();
        params.add("name",name);
        params.add("qq",qq);
        Request request = new Request.Builder()
                .url(url)
                .post(params.build())
                .build();
        OkHttpClient client=new OkHttpClient();
        Thread addThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.i("---","--------第一次————————————————");
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    String data = jsonObject.optString("data",null);
                    Log.i(data,data);
                    if (!data.isEmpty()){
                        Log.i("data",data);
                        JSONObject object = new JSONObject(data);
                        int id = object.optInt("userId",0);
                        String name = object.optString("userName",null);
                        String photo = object.optString("userPhoto",null);
                        String phone = object.optString("userPhone",null);
                        String qq = object.optString("userQq",null);
                        String zfb = object.optString("userZfb",null);
                        resultUser.setUserId(id);
                        resultUser.setUserName(name);
                        resultUser.setUserPhoto(photo);
                        resultUser.setUserPhone(phone);
                        resultUser.setUserQq(qq);
                        resultUser.setUserZfb(zfb);
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        addThread.start();
        try {
            addThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        MyHttpUtil.sendOkhttpPostRequest(url,params.build(), new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.i("zjc",e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String responseData = response.body().string();
//                Log.i("responseData",responseData);
//                try {
//                    JSONObject jsonObject = new JSONObject(responseData);
//                    String data = jsonObject.optString("data",null);
//                    Log.i(data,data);
//                    if (!data.isEmpty()){
//                        Log.i("data",data);
//                        JSONObject object = new JSONObject(data);
//                        int id = object.optInt("userId",0);
//                        String name = object.optString("userName",null);
//                        String photo = object.optString("userPhoto",null);
//                        String phone = object.optString("userPhone",null);
//                        String qq = object.optString("userQq",null);
//                        String zfb = object.optString("userZfb",null);
//                        resultUser.setUserId(id);
//                        resultUser.setUserName(name);
//                        resultUser.setUserPhoto(photo);
//                        resultUser.setUserPhone(phone);
//                        resultUser.setUserQq(qq);
//                        resultUser.setUserZfb(zfb);
//                        Log.i("resultUser", String.valueOf(resultUser));
//                        User user = resultUser;
//                        Log.i("user", user.getUserName());
//                        if (resultUser != null){
//                            Log.i("user", user.getUserName());
//                            signinActivity.loginCallback(resultUser);
//                        }
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        Log.i("---","--------第二次————————————————");
        return resultUser;
    }
}
